﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

/******************************************************************************************
 ** 文件名：	UserComponent.cs
 ** 创建人：	Tony Mucor
 ** 日期：	2013-07-24
 ** 描述：	用户控制类
 *****************************************************************************************/

/// <summary>
/// UserComponent 用户控制类
/// </summary>
public class UserComponent
{
	private UserDB clsData;

	public UserComponent()
	{
		clsData = new UserDB();
	}

	#region 方法 2013-07-24
	private User DataToEntity(DataRow dr)
	{
		User clsUser;
		UserGroupComponent clsUserGroupComponent;
		DepartmentComponent clsDepartmentComp = new DepartmentComponent();

		clsUser = new User();
		clsUserGroupComponent = new UserGroupComponent();

		if (dr != null)
		{
			clsUser.ID = Convert.ToInt32(dr["Users_ID"]);
			clsUser.Username = dr["Users_Username"].ToString();
			clsUser.Password = dr["Users_Password"].ToString();
			clsUser.UserGroupID = Convert.ToInt32(dr["Users_GroupID"]);
			clsUser.Status = Convert.ToInt32(dr["Users_Status"]);
			clsUser.Name = dr["Users_Name"].ToString();
			clsUser.DepartmentID = Convert.ToInt32(dr["Users_DepartmentID"]);

			clsUser.UserGroup = clsUserGroupComponent.GetOneByID(clsUser.UserGroupID).UserGroupName;
			clsUser.ColumnIDs = clsUserGroupComponent.GetOneByID(clsUser.UserGroupID).UserGroupColumnsID;

			clsUser.Department = clsDepartmentComp.GetOne(clsUser.DepartmentID).Name;
		}

		return clsUser;
	}
	private void EntityToData(User clsUser)
	{
		clsData.arrTableColumnValue[0] = clsUser.ID.ToString();
		clsData.arrTableColumnValue[1] = clsUser.Username;
		clsData.arrTableColumnValue[2] = clsUser.Password;
		clsData.arrTableColumnValue[3] = clsUser.UserGroupID.ToString();
		clsData.arrTableColumnValue[4] = clsUser.Status.ToString();
		clsData.arrTableColumnValue[5] = clsUser.Name.ToString();
		clsData.arrTableColumnValue[6] = clsUser.DepartmentID.ToString();
	}
	#endregion

	#region 添加、修改、删除 2013-07-24
	public Boolean Add(User clsUser)
	{
		EntityToData(clsUser);
		return clsData.Add();
	}

	public Boolean Modify(User clsUser)
	{
		EntityToData(clsUser);
		return clsData.Modify();
	}

	public Boolean Delete(User clsUser)
	{
		EntityToData(clsUser);
		return clsData.Delete();
	}
	#endregion

	#region 查询 2013-07-24
	public User GetOneByID(int nID)
	{
		DataRow dr;

		dr = clsData.QueryOneToDataRow(nID);

		return DataToEntity(dr);
	}

	public List<User> QueryByID(int nID)
	{
		List<User> list;

		list = new List<User>();

		list.Add(GetOneByID(nID));

		return list;
	}

	public List<User> QueryAll()
	{
		DataTable dt;
		List<User> list;

		list = new List<User>();
		dt = clsData.QueryAllToDataTable();

		foreach (DataRow dr in dt.Rows)
		{
			list.Add(DataToEntity(dr));
		}

		return list;
	}

	/**********************************************************************************************
	 ** 功能描述：	根据用户组ID查询属于该用户组的用户
	 ** 输入参数：
	 ** 输出参数：
	 ** 返回值：
	 ** 作者：	Tony Mucor
	 ** 日期：	2013-07-24
	 ** 备注：
	 *********************************************************************************************/
	public List<User> QueryByUserGroupID(int nUserGroupID)
	{
		List<User> list;
		DataTable dt;

		list = new List<User>();
		dt = clsData.QueryByUserGroupID(nUserGroupID);
		foreach (DataRow dr in dt.Rows)
		{
			list.Add(DataToEntity(dr));
		}

		return list;
	}

	/**********************************************************************************************
	 ** 功能描述：	根据用户名和密码查询用户，如果查不到返回null
	 ** 输入参数：
	 ** 输出参数：	clsUser
	 ** 返回值：
	 ** 作者：	Tony Mucor
	 ** 日期：	2013-07-26
	 ** 备注：
	 *********************************************************************************************/
	public User Login(string strUsername, string strPassowrd)
	{
		User clsUser;
		DataTable dt;

		clsUser = new User();

		dt = clsData.QueryByUsernamePassword(strUsername, strPassowrd);
		if(dt.Rows.Count > 0)
		{
			clsUser = DataToEntity(dt.Rows[0]);
		}
		else
		{
			clsUser = null;
		}

		return clsUser;
	}
	#endregion
}
